// This file is part of OpenTSDB.
// Copyright (C) 2017 TeleDB Development Group.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.tsd.http;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;

import java.util.Map;

/**
 * Service 返回结果处理类
 *
 */
public class ServiceResponse {

    private ServiceResponse(){}

    private Boolean result;
    private Boolean error;
    private String message;
    private Map<String, String> response;
    private HttpResponseStatus status;

    public static ServiceResponse.Builder builder(Boolean result){
        return new Builder(result);
    }

    public static ServiceResponse.Builder builder(HttpResponseStatus status){
        return new Builder(status);
    }

    public static class Builder{
        private Boolean result = true;
        private Boolean error = false;
        private String message = "";
        private Map<String, String> response = Maps.newHashMap();
        private HttpResponseStatus status = HttpResponseStatus.OK;

        public Builder(Boolean result){
            this.result = result;
        }

        public Builder(HttpResponseStatus status){
            this.status = status;
            // 返回异常
            if(status.getCode() >= 300) this.result = false;
        }

        public Builder error(Boolean error){
            this.error = error;
            return this;
        }

        public Builder message(String message){
            this.message = message;
            return this;
        }

        public Builder response(Map<String, String> response){
            this.response = response;
            return this;
        }

        public Builder status(HttpResponseStatus status){
            this.status = status;
            return this;
        }

        public ServiceResponse build(){
            ServiceResponse serviceResponse = new ServiceResponse();
            serviceResponse.result = this.result;
            serviceResponse.error = this.error;
            serviceResponse.message = this.message;
            serviceResponse.response = this.response;
            serviceResponse.status = this.status;

            return serviceResponse;
        }
    }

    public Boolean getResult() {
        return result;
    }

    public Boolean getError() {
        return error;
    }

    public String getMessage() {
        return message;
    }

    public Map<String, String> getResponse() {
        return response;
    }

    public HttpResponseStatus getStatus() {
        return status;
    }

    public Map<String,String> getResMap(){
        Map<String, String> res = Maps.newHashMap();

        // 保持状态一致
        if(status != null && status.getCode() >= 300){
            result = false;
            error = true;
        }

        res.put("result",String.valueOf(result));
        if(error) res.put("error",String.valueOf(error));
        if(!Strings.isNullOrEmpty(message))
            res.put("message",message);
        if(response!=null && response.size()>0)
            res.putAll(response);

        return res;
    }
}
